/**   
 * Model Created on 2022年4月28日
 *   
 * Copyright (c) 2018 by Kunrong.HOo.
 */ 
package com.seaside.park.service;

import com.alibaba.fastjson.JSON;
import com.seaside.parkCommon.service.HttpService;
import com.seaside.parkCommon.vo.yz.BerthInOutVO;
import com.seaside.parkCommon.vo.yz.PunitoryParkVO;
import com.seaside.parkCommon.vo.yz.RequestParam;
import com.seaside.parkCommon.vo.yz.RespResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

/**
 * 通知服务
 * 
 * @author <a href="mailto:hoocoral@hotmail.com">Kunrong.HOo</a>
 * @version 1.0
 */
@Slf4j
@Service
public class NotifyService extends HttpService {

	@Value("${app.yztc.host}")
	private String yztcHostStr;

	@Value("${app.yztc.url.berthInOut}")
	private String yztcBerthInOutUrl;
	
	@Value("${app.yztc.url.punitoryPark}")
	private String yztcPunitoryParkUrl;

    @Autowired
    @Qualifier("asyncServiceExecutor")
    private Executor executor;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

	/**
	 * 车辆出、入车位通知接口
	 * @param berthInOutVO
	 */
    public void postBerthInOutNotify(BerthInOutVO berthInOutVO) {

        // 设置参数
        RequestParam<BerthInOutVO> param = new RequestParam<>(berthInOutVO);
        param.sign();

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
    	
    	HttpEntity<RequestParam<BerthInOutVO>> httpEntity = new HttpEntity<>(param, headers);
        // 请求并解析数据
        String requestUrl = yztcHostStr + yztcBerthInOutUrl;
        ResponseEntity<RespResult> respRet = restTemplate.postForEntity(requestUrl, httpEntity, RespResult.class);
        if (respRet != null) {
        	RespResult queryResult = respRet.getBody();
        	
        	log.info(JSON.toJSONString(queryResult));

        	// TODO：这里做逻辑处理
        }
    }
    
    /**
     * 处罚性计费变更通知接口
     * @param punitoryParkVO
     */
    public void postPunitoryParkNotify(PunitoryParkVO punitoryParkVO) {
    	// 设置参数
        RequestParam<PunitoryParkVO> param = new RequestParam<>(punitoryParkVO);
        param.sign();

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
    	
    	HttpEntity<RequestParam<PunitoryParkVO>> httpEntity = new HttpEntity<>(param, headers);
        // 请求并解析数据
        String requestUrl = yztcHostStr + yztcPunitoryParkUrl;
        ResponseEntity<RespResult> respRet = restTemplate.postForEntity(requestUrl, httpEntity, RespResult.class);
        if (respRet != null) {
        	RespResult queryResult = respRet.getBody();
        	
        	log.info(JSON.toJSONString(queryResult));

        	// TODO：这里做逻辑处理
        }
    }

    /**
     * 通知云智平台落位或离开
     * @param parkNo
     * @param parkZoneNo
     * @param carPlate
     * @param noticeType
     */
    public void NotifyYZ(String parkNo, String parkZoneNo,String carPlate, String noticeType) {
        //通知云智已经停车
        CompletableFuture<Boolean> supplyAsync = CompletableFuture.supplyAsync(() -> {
            BerthInOutVO berthInOutVO = new BerthInOutVO();
            berthInOutVO.setBerthNumber(parkNo);
            berthInOutVO.setParkId(parkZoneNo);
            berthInOutVO.setCarPlate(carPlate);
            berthInOutVO.setDateTime(sdf.format(new Date()));
            berthInOutVO.setEventTime(sdf.format(new Date()));
            berthInOutVO.setEventType(noticeType);
            postBerthInOutNotify(berthInOutVO);
            return true;
        }, executor);
    }
}
